<template>
  <el-form ref="form" :model="form">
    <el-form-item class="box_form">
      <!-- <el-form-item class="number" label="创建人姓名">
        <el-input
          placeholder="请输入创建人姓名"
          v-model="form.username"
        ></el-input>
      </el-form-item>
      <el-form-item class="number" label="创建人ID">
        <el-input placeholder="请输入创建人ID" v-model="form.userid"></el-input>
      </el-form-item> -->
      <el-form-item class="number" label="发送机制编码">
        <el-input
          placeholder="请输入发送机制编码"
          v-model="form.mechanismCode"
        ></el-input>
      </el-form-item>
      <el-form-item class="number" label="发送机制描述">
        <el-input
          placeholder="请输入发送机制描述"
          v-model="form.mechanismDesc"
        ></el-input>
      </el-form-item>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">添加</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addSend } from "../../api.js";
export default {
  data() {
    return {
      form: {
        username: "",
        userid: "",
        mechanismCode: "",
        mechanismDesc: ""
      }
    };
  },
  mounted() {
    this.restaurants = this.loadAll();
    this.rescontract = this.loadAll();
  },
  methods: {
    handleSelect(item) {
      console.log(item);
    },
    contractSelect(data) {
      console.log(data);
    },
    onSubmit() {
      let num = this.form.mechanismCode;
      var format = true; //编码机制判断
      for (var i = 0; i < num.length; i++) {
        if (num.charAt(i) != 0 && num.charAt(i) != 1) {
          this.$message({
            message: "消息机制编码格式不正确，请重新输入",
            type: "warning"
          });
          format = false;
          return;
        }
      }

      if (num.length != 11) {
        this.$message({
          message: "消息机制编码格式不正确，请重新输入",
          type: "warning"
        });
        format = false;
      }

      if (num == "00000000000") {
        this.$message({
          message: "无效的机制编码",
          type: "warning"
        });
        format = false;
      }
      if (format) {
        if (this.form.mechanismDesc == "") {
          this.$message({
            message: "机制描述不能为空",
            type: "warning"
          });
        } else {
          addSend({
            createUserId: sessionStorage.getItem("userid"),
            createUserName: sessionStorage.getItem("username"),
            mechanismCode: this.form.mechanismCode,
            mechanismDesc: this.form.mechanismDesc
          })
            .then(res => {
              this.form.mechanismDesc = '';
              this.form.mechanismCode = '';
              this.$emit("close", false);
              this.$message({
                showClose: true,
                message: res.remsg,
                type: "success"
              });
            })
            .catch((res = {}));
        }
      }
    }
  }
};
</script>

<style scoped lang="less">
.el-form {
  /deep/.el-form-item__content {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-left: 0 !important;
    .number {
      margin-right: 5%;
      margin-top: 40px;
    }
    .el-input {
      width: 40vh;
    }
    .el-form-item__label {
      font-size: 16px;
      text-align: right;
      width: 25%;
    }
  }
}
.box_form {
  margin: 40px 0 50px !important;
}
.box_textarea {
  width: 70% !important;
  /deep/.el-textarea__inner {
    height: 15vh;
    font-size: 18px !important;
  }
}
</style>
